//
//  WLCltViewLayout.m
//  SimpleSrore
//
//  Created by 梁永升 on 2022/11/22.
//  Copyright © 2022 高广校. All rights reserved.
//

#import "WLCltViewLayout.h"
@interface WLCltViewLayout ()

@property (nonatomic,strong) NSMutableArray *attributeArr;
@property (nonatomic,assign) float lastLeftY;
@property (nonatomic,assign) float lastRightY;
@end
@implementation WLCltViewLayout

//初始化操作
-(void)prepareLayout{
    [super prepareLayout];
    _lastLeftY = 6*Device_Ccale;
    _lastRightY = 6*Device_Ccale;
    [self.attributeArr removeAllObjects];
    
    NSInteger rowCount = [self.collectionView numberOfItemsInSection:0];
    for (int i = 0 ; i < rowCount; i++) {
        UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
        [self.attributeArr addObject:attributes];
    }
}

//决定了cell的排布方式
-(NSArray <UICollectionViewLayoutAttributes *>*)layoutAttributesForElementsInRect:(CGRect)rect{
    if (self.attributeArr.count == 0) {
        return nil;
    }
    return self.attributeArr;
}

-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    float itemWideh = (SCREEN_WIDTH - 36*Device_Ccale) /2;
    UICollectionViewLayoutAttributes *layoutAttribut = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    CGSize itemSize = [self.delegate waterFlowLayout:self sizeForItemAtIndexPath:indexPath];

    layoutAttribut.frame = CGRectMake(indexPath.row % 2 == 0 ? 12*Device_Ccale : itemWideh + 24*Device_Ccale, indexPath.row % 2 == 0 ? self.lastLeftY : self.lastRightY, itemSize.width, itemSize.height);
    if (indexPath.row % 2 == 0){
        self.lastLeftY += itemSize.height + 12*Device_Ccale;
    }else{
        self.lastRightY += itemSize.height + 12*Device_Ccale;
    }
    return layoutAttribut;
}

-(CGSize)collectionViewContentSize{
    return CGSizeMake(SCREEN_WIDTH, MAX(self.lastLeftY , self.lastRightY));
}

-(NSMutableArray *)attributeArr{
    if (!_attributeArr) {
        _attributeArr = [NSMutableArray array];
    }
    return _attributeArr;
}

@end
